package com.leetcode.p200;

public class Test209 {

    public int minSubArrayLen(int target, int[] nums) {
        int min = Integer.MAX_VALUE;

        int start = 0;
        int end = 0;

        int n = nums.length;

        int sum = 0;

        while (end < n) {
            sum += nums[end];

            while (sum >= target) {
                min = Math.min(min, end - start + 1);
                sum -= nums[start];
                start++;
            }
            end++;
        }
        if (min == Integer.MAX_VALUE) {
            return 0;
        }
        return min;
    }

    public static void main(String[] args) {
//        int[] arr = new int[]{4, 3, 12, 4, 0, 18, 9};
        int[] arr = new int[]{2, 3, 1, 2, 4, 3};
//        int[] arr = new int[]{1, 1};
//        int[] arr = new int[]{1, 4, 4};

        Test209 test = new Test209();
        int result = test.minSubArrayLen(7, arr);
        System.out.println(result);
    }

}
